﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;

namespace Smartmanufact
{
    public partial class MainView : Form
    {
        public static bool online = false;    // 是否有登陆的，默认值为否
        public static string username = "";   // 登陆的用户名
        public static string limit = "";      // 登陆用户的权限 

        //  所有界面
        login login_View = new login();
        usermanager User_View = new usermanager();
        cargoinfomanager Cargoinfo_View = new cargoinfomanager();
        customer_manager Customer_View = new customer_manager();
        Shelf_info_view Shelf_View = new Shelf_info_view();
        Cargo_View cargoview = new Cargo_View();
        Cargo_Search_View cargo_search_view = new Cargo_Search_View();
        Connetion_View connetc_view = new Connetion_View();
        Shebiexinxi shebeixinxi_view = new Shebiexinxi();
        Rectangle rect;


        // 存放按钮的数组
        ArrayList button_list = new ArrayList();

        // 数据库
        public static SQL_Connection mysql = new SQL_Connection("server=.;database=SmartManufact;uid=sa;pwd=ncl5722354");
        public MainView()
        {
            InitializeComponent();
            login_View.mv = this;
            rect=System.Windows.Forms.SystemInformation.VirtualScreen;
            this.Top = 0;
            this.Left = 0;
            this.Width = rect.Width;
            this.Height = rect.Height;

            // 几个按钮的位置
            pictureBox_yonghuguanli.Top = (int)(this.Height * 0.105);
            pictureBox_yonghuguanli.Height = (int)(this.Height * 0.072);
            pictureBox_yonghuguanli.Width = (int)(this.Width * 0.072);
            pictureBox_yonghuguanli.Left = (int)(this.Width * 0.05);
            button_list.Add(pictureBox_yonghuguanli);

            pictureBox_kehuguanli.Top = (int)(this.Height * 0.105);
            pictureBox_kehuguanli.Width = pictureBox_yonghuguanli.Width;
            pictureBox_kehuguanli.Height = (int)(this.Height * 0.072);
            pictureBox_kehuguanli.Left = (int)(this.Width * 0.05 + pictureBox_kehuguanli.Width);
            button_list.Add(pictureBox_kehuguanli);

            pictureBox_huoquguanli.Top = (int)(this.Height * 0.105);
            pictureBox_huoquguanli.Width = pictureBox_yonghuguanli.Width;
            pictureBox_huoquguanli.Height = (int)(this.Height * 0.072);
            pictureBox_huoquguanli.Left = (int)(this.Width * 0.05 + 2 * pictureBox_kehuguanli.Width);
            button_list.Add(pictureBox_huoquguanli);

            pictureBox_huojiaxinxi.Top = (int)(this.Height * 0.105);
            pictureBox_huojiaxinxi.Width = pictureBox_yonghuguanli.Width;
            pictureBox_huojiaxinxi.Height=(int)(this.Height*0.072);
            pictureBox_huojiaxinxi.Left=(int)(this.Width*0.05+3*pictureBox_kehuguanli.Width);
            button_list.Add(pictureBox_huojiaxinxi);

            pictureBox_huojiaxianshi.Top=(int)(this.Height*0.105);
            pictureBox_huojiaxianshi.Width=pictureBox_yonghuguanli.Width;
            pictureBox_huojiaxianshi.Height=(int)(this.Height*0.072);
            pictureBox_huojiaxianshi.Left=(int)(this.Width*0.05+4*pictureBox_kehuguanli.Width);
            button_list.Add(pictureBox_huojiaxianshi);

            pictureBox_huowupandian.Top=(int)(this.Height*0.105);
            pictureBox_huowupandian.Width=pictureBox_yonghuguanli.Width;
            pictureBox_huowupandian.Height=(int)(this.Height*0.072);
            pictureBox_huowupandian.Left=(int)(this.Width*0.05+5*pictureBox_huowupandian.Width);
            button_list.Add(pictureBox_huowupandian);

            pictureBox_tongxunshezhi.Top=(int)(this.Height*0.105);
            pictureBox_tongxunshezhi.Width=pictureBox_yonghuguanli.Width;
            pictureBox_tongxunshezhi.Height=(int)(this.Height*0.072);
            pictureBox_tongxunshezhi.Left=(int)(this.Width*0.05+6*pictureBox_tongxunshezhi.Width);
            button_list.Add(pictureBox_tongxunshezhi);

            pictureBox_shebeixiangqing.Top=(int)(this.Height*0.105);
            pictureBox_shebeixiangqing.Width=pictureBox_yonghuguanli.Width;
            pictureBox_shebeixiangqing.Height=(int)(this.Height*0.072);
            pictureBox_shebeixiangqing.Left=(int)(this.Width*0.05+7*pictureBox_yonghuguanli.Width);
            button_list.Add(pictureBox_shebeixiangqing);

            //panel1.Top = button_usermanager.Top+button_usermanager.Height+(int)(rect.Height*0.1);
            panel1.Top = 0;
            panel1.Left = 0;
            panel1.Width = rect.Width;
            panel1.Height = rect.Height-panel1.Top;

            toolTip1.AutoPopDelay = 5000;
            toolTip1.InitialDelay = 100;
            toolTip1.ReshowDelay = 50;
            toolTip1.ShowAlways = true;

            // 初始化所有的窗口
            init_form(login_View);
            init_form(User_View);
            init_form(Cargoinfo_View);
            init_form(Customer_View);
            init_form(cargoview);
            init_form(Shelf_View);
            init_form(cargo_search_view);
            init_form(connetc_view);
            init_form(shebeixinxi_view);
            Hide_ALL_Button();
            Select_Button(0);
        }

        public void Set_Panel()
        {
            panel1.Top = button_usermanager.Top + button_usermanager.Height + (int)(rect.Height * 0.1);
        }

        public void Show_ALL_Button()
        {
            pictureBox_yonghuguanli.Visible = true;
            pictureBox_kehuguanli.Visible = true;
            pictureBox_huoquguanli.Visible = true;
            pictureBox_huojiaxinxi.Visible = true;
            pictureBox_huojiaxianshi.Visible = true;
            pictureBox_huowupandian.Visible = true;
            pictureBox_tongxunshezhi.Visible = true;
            pictureBox_shebeixiangqing.Visible = true;
        }

        public void Hide_ALL_Button()
        {
            pictureBox_yonghuguanli.Visible = false;
            pictureBox_kehuguanli.Visible = false;
            pictureBox_huoquguanli.Visible = false;
            pictureBox_huojiaxinxi.Visible = false;
            pictureBox_huojiaxianshi.Visible = false;
            pictureBox_huowupandian.Visible = false;
            pictureBox_tongxunshezhi.Visible = false;
            pictureBox_shebeixiangqing.Visible = false;
        }
 
        public void Select_Button(int i)
        {
            pictureBox_yonghuguanli.Image = Smartmanufact.Properties.Resources.用户管理3;
            pictureBox_kehuguanli.Image = Smartmanufact.Properties.Resources.入库管理;
            pictureBox_huoquguanli.Image = Smartmanufact.Properties.Resources.客户管理4;
            pictureBox_huojiaxinxi.Image = Smartmanufact.Properties.Resources.货架管理4;
            pictureBox_huojiaxianshi.Image = Smartmanufact.Properties.Resources.订单管理;
            pictureBox_huowupandian.Image = Smartmanufact.Properties.Resources.货物盘点3;
            pictureBox_tongxunshezhi.Image = Smartmanufact.Properties.Resources.通讯设置3;
            pictureBox_shebeixiangqing.Image = Smartmanufact.Properties.Resources.设备详情2;
            if (i == 1) pictureBox_yonghuguanli.Image = Smartmanufact.Properties.Resources.用户管理_白3;
            if (i == 2) pictureBox_kehuguanli.Image = Smartmanufact.Properties.Resources.入库管理_白;
            if (i == 3) pictureBox_huoquguanli.Image = Smartmanufact.Properties.Resources.客户管理_白3;
            if (i == 4) pictureBox_huojiaxinxi.Image = Smartmanufact.Properties.Resources.货物信息_白2;
            if (i == 5) pictureBox_huojiaxianshi.Image = Smartmanufact.Properties.Resources.订单管理_白;
            if (i == 6) pictureBox_huowupandian.Image = Smartmanufact.Properties.Resources.货物盘点_白2;
            if (i == 7) pictureBox_tongxunshezhi.Image = Smartmanufact.Properties.Resources.通讯设置_白2;
            if (i == 8) pictureBox_shebeixiangqing.Image = Smartmanufact.Properties.Resources.设备详情_白2;
        }


        private void MainView_Load(object sender, EventArgs e)
        {
            // 创建数据库所要用到的表
            mysql.excute_sql("CREATE table usertable(username nvarchar(50) PRIMARY KEY,password nvarchar(50),pression nvarchar(50))");              // 用户表
            mysql.excute_sql("CREATE table cargoinfotable(cargoinfoID nvarchar(50) PRIMARY KEY,cargoinfoname nvarchar(50),note nvarchar(MAX))");     // 货物信息表
            mysql.excute_sql("CREATE table customertable(customerID nvarchar(50) PRIMARY KEY,name nvarchar(50),company nvarchar(MAX),phone nvarchar(50),email nvarchar(50))");  // 客户表
            mysql.excute_sql("CREATE table cargotable(cargoID nvarchar(50) PRIMARY KEY,cargoname nvarchar(50),cargokindid nvarchar(50),cargostate nvarchar(50),cargo_saveinfo nvarchar(MAX),shelf nvarchar(50),shelf_row_index nvarchar(50),shelf_col_index nvarchar(50),customername nvarchar(50),username nvarchar(50),savetime nvarchar(50),manual nvarchar(50))");   // 货物表
            mysql.excute_sql("CREATE table entrepotinfo(enterpotname nvarchar(50) PRIMARY KEY,col_count nvarchar(50),row_count nvarchar(50))");    // 仓库信息表
            Show_form(login_View);

            Frame newframe = new Frame();
        }

        private void init_form(Form myform)
        {
            // 初始化界面信息
            myform.TopLevel = false;      //设置为非顶级控件
           
        }

        private void Show_form(Form myform)
        {
            panel1.Controls.Clear();
            panel1.Controls.Add(myform);
            myform.Top = 0;
            myform.Left = 0;
            myform.Width = panel1.Width;
            myform.Height = panel1.Height;
            myform.Show();
        }

        private void button_usermanager_Click(object sender, EventArgs e)
        {
            if (online == false) { MessageBox.Show("请登陆之后再操作！"); return; }
            if (online == true && limit != "admin") { MessageBox.Show("权限不足"); return; }
            if(online==true && limit=="admin")
            {
                 // 可以进入用户编辑界面
                Show_form(User_View);
            }
        }

        private void button_exit_Click(object sender, EventArgs e)
        {
            Show_form(login_View);
            MainView.online = false;
            MainView.username = "";
            MainView.limit = "";
            login_View.Clear_PassWord();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (online == false) { MessageBox.Show("请登陆之后再操作！"); return; }
            if (online == true && limit != "admin" ) { MessageBox.Show("权限不足"); return; }
            if (online == true && limit == "admin")
            {
                // 可以进入货物信息管理
                Show_form(Cargoinfo_View);
            }
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void button_customer_Click(object sender, EventArgs e)
        {
            if (online == false) { MessageBox.Show("请登陆之后再操作！"); return; }
            if (online == true && limit != "admin") { MessageBox.Show("权限不足"); return; }
            if(online==true && limit=="admin")
            {
                Show_form(Customer_View);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (online == false) { MessageBox.Show("请登陆之后再操作！"); return; }
            if(online==true && (limit=="admin"||limit=="user"))
            {
                Show_form(cargoview);
            }
        }

        private void button_shelf_info_Click(object sender, EventArgs e)
        {
            if (online == false) { MessageBox.Show("请登陆之后再操作！"); return; }
            if (online == true && limit != "admin") { MessageBox.Show("权限不足！"); return; }
            if (online == true && limit == "admin") 
            {
                Show_form(Shelf_View);
            }
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            if(online==false)
            {
                MessageBox.Show("请登录之后再操作！");
                return;
            }
            if(online==true && (limit=="admin" || limit=="user"))
            {
                Show_form(cargo_search_view);
            }
        }

        private void button_connect_config_Click(object sender, EventArgs e)
        {
            if(online==false)
            {
                MessageBox.Show("请登陆之后再操作！");
            }
            if(online==true && (limit=="admin" || limit=="user"))
            {
                Show_form(connetc_view);
            }
        }

        private void button_usermanager_DragEnter(object sender, DragEventArgs e)
        {
            
        }

        private void button_usermanager_MouseEnter(object sender, EventArgs e)
        {
            Button mybutton = (Button)sender;
            toolTip1.SetToolTip(mybutton, "用户管理");
        }

        private void button1_MouseEnter(object sender, EventArgs e)
        {
            Button mybutton = (Button)sender;
            toolTip1.SetToolTip(mybutton,"货物信息");
        }

        private void button_customer_MouseEnter(object sender, EventArgs e)
        {
            Button mybutton = (Button)sender;
            toolTip1.SetToolTip(mybutton, "用户管理");
        }

        private void button_shelf_info_MouseEnter(object sender, EventArgs e)
        {
            Button mybutton = (Button)sender;
            toolTip1.SetToolTip(mybutton, "货架信息");
        }

        private void button_cargoview_MouseEnter(object sender, EventArgs e)
        {
            Button mybutton = (Button)sender;
            toolTip1.SetToolTip(mybutton, "货架显示");
        }

        private void button2_MouseEnter(object sender, EventArgs e)
        {
            Button mybutton = (Button)sender;
            toolTip1.SetToolTip(mybutton, "货物清点");
        }

        private void button_connect_config_MouseEnter(object sender, EventArgs e)
        {
            Button mybutton = (Button)sender;
            toolTip1.SetToolTip(mybutton, "系统配置");
        }

        private void button_exit_MouseEnter(object sender, EventArgs e)
        {
            Button mybutton = (Button)sender;
            toolTip1.SetToolTip(mybutton, "关闭");
        }

        public void Show_Init_View_Once()
        {
            if (online == false) { MessageBox.Show("请登陆之后再操作！"); return; }
            if (online == true && limit != "admin") { MessageBox.Show("权限不足"); return; }
            if (online == true && limit == "admin")
            {
                // 可以进入用户编辑界面
                Show_form(User_View);
            }
        }

        private void pictureBox_yonghuguanli_Click(object sender, EventArgs e)
        {
            Select_Button(1);
            if (online == false) { MessageBox.Show("请登陆之后再操作！"); return; }
            if (online == true && limit != "admin") { MessageBox.Show("权限不足"); return; }
            if (online == true && limit == "admin")
            {
                // 可以进入用户编辑界面
                Show_form(User_View);
            }
        }

        private void pictureBox_huoquguanli_Click(object sender, EventArgs e)
        {
            Select_Button(3);
            if (online == false) { MessageBox.Show("请登陆之后再操作！"); return; }
            if (online == true && limit != "admin") { MessageBox.Show("权限不足"); return; }
            if (online == true && limit == "admin")
            {
                Show_form(Customer_View);
            }
        }

        private void pictureBox_kehuguanli_Click(object sender, EventArgs e)
        {
            Select_Button(2);
            if (online == false) { MessageBox.Show("请登陆之后再操作！"); return; }
            if (online == true && limit != "admin") { MessageBox.Show("权限不足"); return; }
            if (online == true && limit == "admin")
            {
                // 可以进入货物信息管理
                Show_form(Cargoinfo_View);
            }
        }

        private void pictureBox_huojiaxinxi_Click(object sender, EventArgs e)
        {
            Select_Button(4);
            if (online == false) { MessageBox.Show("请登陆之后再操作！"); return; }
            if (online == true && limit != "admin") { MessageBox.Show("权限不足！"); return; }
            if (online == true && limit == "admin")
            {
                Show_form(Shelf_View);
            }
            
        }

        private void pictureBox_huojiaxianshi_Click(object sender, EventArgs e)
        {
            Select_Button(5);
            if (online == false) { MessageBox.Show("请登陆之后再操作！"); return; }
            if (online == true && (limit == "admin" || limit == "user"))
            {
                Show_form(cargoview);
            }
        }

        private void pictureBox_huowupandian_Click(object sender, EventArgs e)
        {
            Select_Button(6);
            if (online == false)
            {
                MessageBox.Show("请登录之后再操作！");
                return;
            }
            if (online == true && (limit == "admin" || limit == "user"))
            {
                Show_form(cargo_search_view);
            }
        }

        private void pictureBox_tongxunshezhi_Click(object sender, EventArgs e)
        {
            Select_Button(7);
            if (online == false)
            {
                MessageBox.Show("请登陆之后再操作！");
            }
            if (online == true && (limit == "admin" || limit == "user"))
            {
                Show_form(connetc_view);
            }
            
        }

        private void pictureBox_shebeixiangqing_Click(object sender, EventArgs e)
        {
            Select_Button(8);
            if(online==false)
            {
                MessageBox.Show("请登陆之后再操作！");
            }
            else
            {
                Show_form(shebeixinxi_view);
            }
        }
    }
}
